package com.ygxsk.carhome.js;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;

import com.blankj.utilcode.util.PathUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.tencent.smtt.sdk.WebView;
import com.ygxsk.carhome.R;
import com.ygxsk.carhome.constant.ConstantSign;
import com.ygxsk.carhome.utils.DemoImage;
import com.ygxsk.integrated.retrofit.Parameterkey;

import java.io.File;

/**
 * Autuhor:Administrator
 * Time:2021/5/10 0010  14:14
 * Description: this is 自定义的 一个接口接入
 */
public class MJavascriptInterface {

    private Context context;

    private Activity activity;

    /**
     * 添加webview的内容
     */
    private WebView mWebView;

    public MJavascriptInterface(Context context, Activity activity) {
        this.context = context;
        this.activity = activity;
    }

    public MJavascriptInterface(Context context, Activity activity, WebView mWebView) {
        this.context = context;
        this.activity = activity;
        this.mWebView = mWebView;
    }

    @android.webkit.JavascriptInterface
    public void openImage(String imgURL) {
        //public void openImage(String imgURL, String webViewPageUrl) {
        //添加一个 接口地址的内容，如果是这个接口，那么就不要去加载大图

        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {

                //LogUtils.d("itchen--JavascriptInterface--openImage----当前获取的界面地址是--看大图的-webViewPageUrl--->" + webViewPageUrl);

                //ArrayList<String> arrayList = new ArrayList<>();

                /*if (!TextUtils.isEmpty(webViewPageUrl)) {

                    //如果是获取售水列表，那末，就不要点击查看大图
                    if (TextUtils.equals(webViewPageUrl, Constants.getInstance().getComboProductUrl())) {

                        return;
                    }

                }*/

                if (!TextUtils.isEmpty(imgURL)) {

//                    LogUtils.d("itchen----->点击查看大图的接入-->" + imgURL);
                    //itchen----->点击查看大图的接入-->
                    //data:image/png;base64

                    //家装团购的两类图片的地址文件夹
                    //http://222.222.17.184:8881/resource/image/houseTypeImg/20211215/2021121513584110214304.jpg
                    //http://222.222.17.184:8881/resource/image/houseProjectImg/20211215/2021121513520662857899.jpeg
                    //http://222.222.17.184:8881/resource/company/logo/20211215/2021121513483959562136.png
                    //2022年8月2日17:52:09 发现bug 有的小图片包含base64 ,竟然也能查看大图，给过滤掉；
                    //itchen----->点击查看大图的  接入 
                    //|| imgURL.contains("video")
                    //示例代码：https://devyun.guorenpcic.com
                    //如果图片接入内包含有保险的内容，就不执行查看大图.
                    if (imgURL.contains("notlook")) {
                        return;
                    }

                    //如果是携带有base64的图片，那么直接转换，因为携带的base64的值太长了
                    if (imgURL.startsWith(Parameterkey.base64png) || imgURL.startsWith(Parameterkey.base64png)) {

                        String urlResult = "";

                        //============================================================================================================
                        //String fileFolder = PathUtils.getExternalDcimPath() + File.separator + "Camera" + File.separator;
                        //国任保险的图片格式是：
                        //itchen----即将触发的下载图片地址为---downLoadUrl----downLoadUrl->
                        // https://underwrite.oss-cn-beijing.aliyuncs.com/rw3T4o09/ixingxiao/d11516d1f90c4ef78dc24007da735ac9.png
                        // ?Expires=1642162826&OSSAccessKeyId=LTAI3qJF5LvvqmHA&Signature=BiR1U0qUNh3p8HjuRZiMY2r8EZU%3D

                        //String fileFolder = PathUtils.getExternalAppFilesPath()+File.separator+"webview"+File.separator;
                        String fileFolder = PathUtils.getExternalAppFilesPath() + File.separator;
                        //如果是国任保险，那么就是后缀PNG格式。
//                        String fileName = "";
//                        if (imgURL.contains("png")) {
//                            fileName = "webnomal" + System.currentTimeMillis() + ".png";
//                        } else {
//                            fileName = "webnomal" + System.currentTimeMillis() + ".jpg";
//                        }

                        //boolean resultValue = DemoImage.base64ToImage(imgBase64, PathUtils.getExternalAppFilesPath() + File.separator + ConstantSign.IMG + TimeUtils.getNowString() + ConstantSign.IMG_POSTFIX);
                        //base64String.split(",")[1]就是获取除"data:image/;base64,"后的地址。
                        boolean resultValue = DemoImage.base64ToImage(imgURL.split(",")[1], fileFolder + TimeUtils.getNowString() + ConstantSign.IMG_POSTFIX);
//                        LogUtils.d(ConstantSign.LOGCAT_NOMAL + fileFolder + TimeUtils.getNowString() + ConstantSign.IMG_POSTFIX);
                        ///storage/emulated/0/Android/data/com.ygxsk.integrated/files/2023-05-05 14:49:20.jpg

                        ///storage/emulated/0/Android/data/com.ygxsk.integrated/files/webview/webnomal1683267293164.jpg
                        urlResult = fileFolder + TimeUtils.getNowString() + ConstantSign.IMG_POSTFIX;

                        //arrayList.add(imgURL);

                        if (resultValue) {

                            Intent intent = new Intent();
                            intent.putExtra("IMG_URL", urlResult);
                            //intent.putExtra("curImageUrl", img);
//                            intent.setClass(activity, PhotoBrowserActivity.class);
                            activity.startActivity(intent);

                        } else {
                            ToastUtils.showShort(R.string.base64tranf);
                        }

                    } else {

                        //arrayList.add(imgURL);
                        Intent intent = new Intent();
                        intent.putExtra("IMG_URL", imgURL);
                        //intent.putExtra("curImageUrl", img);
//                        intent.setClass(activity, PhotoBrowserActivity.class);
                        activity.startActivity(intent);

                    }

                    //http://wuyeapi.91dgj.com/resource/ueditor/image/20210510/1620636985115088898.jpg

                } else {

                    //LogUtils.d("itchen----->点击查看大图的  接入 图片是空的");

                }


            }
        });


    }

    @android.webkit.JavascriptInterface
    public void openVideo(String videoURL) {

        activity.runOnUiThread(new Runnable() {

            @Override
            public void run() {

                //String img = "https://t7.baidu.com/it/u=2537132411,3912460994&fm=193&f=GIF";
                //String title = "我做测试的";
                //String url = "http://wuyeapi.91dgj.com/resource/ueditor/video/20211028/1635405631044006835.mp4";

                /*String img = "https://t7.baidu.com/it/u=2537132411,3912460994&fm=193&f=GIF";
                String title = "我做测试的";
                String url = "http://wuyeapi.91dgj.com/resource/ueditor/video/20211028/1635405631044006835.mp4";

                JzvdStd jzvdStd = new JzvdStd(activity);

                jzvdStd.setUp(url, title, Jzvd.SCREEN_NORMAL);

                Glide.with(activity)//
                        .asDrawable()//
                        .load(img)//
                        .into(jzvdStd.posterImageView);//

                ViewGroup.LayoutParams ll = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT);

                AbsoluteLayout.LayoutParams layoutParams = new AbsoluteLayout.LayoutParams(ll);

                //layoutParams.y = JZUtils.dip2px(activity, 0);
                //layoutParams.x = JZUtils.dip2px(activity, 0);
                //layoutParams.height = JZUtils.dip2px(activity, ScreenUtils.getScreenHeight());
                //layoutParams.width = JZUtils.dip2px(activity, ScreenUtils.getScreenWidth());

                layoutParams.y = 0;
                layoutParams.x = 0;
                layoutParams.height = ScreenUtils.getScreenHeight();
                layoutParams.width = ScreenUtils.getScreenWidth();

                LinearLayout linearLayout = new LinearLayout(activity);
                linearLayout.addView(jzvdStd);

                mWebView.addView(linearLayout, layoutParams);*/

                //不直接的打开H5
                //ActivityUtils.startActivity(VideoBrowserActivity.class);

                //直接的全屏的播放。
                //JzvdStd.startFullscreenDirectly(activity, JzvdStd.class, url, "");

                //可以直接做一个弹窗的内容。
                //ActivityUtils.startActivity(VideoBrowserMyActivity.class);


                //ActivityUtils.startActivity(GSYVideoPlayerBrowserActivity.class);

                //直接播放的内容： 携带播放视频的链接过去
//                JumpUtils.goToVideoPlayer(activity, videoURL, mWebView);

            }
        });

    }

}
